package com.woniuxy.purchasemanager.controller;

import com.woniuxy.commons.entity.purchaseManager.PurchaseOrderGoodsEntity;
import com.woniuxy.commons.entity.purchaseManager.base.PageResult;
import com.woniuxy.commons.entity.purchaseManager.base.QueryPageBean;
import com.woniuxy.commons.entity.purchaseManager.dto.PurchaseOrderGoodsDTO;
import com.woniuxy.commons.result.Result;
import com.woniuxy.commons.utils.PageUtils;
import com.woniuxy.purchasemanager.service.PurchaseOrderGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 采购单商品表
 *
 * @author huangzhichao
 * @email 1205560871@gmail.com
 * @date 2022-04-19 13:00:28
 */
@Api(tags ="采购单商品表")
@RestController
    @RequestMapping("/need/purchaseordergoods")
public class PurchaseOrderGoodsController {

    public static final Logger LOGGER = LoggerFactory.getLogger(PurchaseOrderController.class);

    @Autowired
    private PurchaseOrderGoodsService purchaseOrderGoodsService;

    /**
     * 列表
     */
    @ApiOperation(value="列表")
    @PostMapping("/list")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseOrderGoodsService.queryPage(params);

        return Result.ok(page);
    }

    @ApiOperation(value = "分页查询采购单对应商品信息")
    @PostMapping("/findPage")
    public Result<PageResult<PurchaseOrderGoodsDTO>> findPage(@RequestBody QueryPageBean queryPageBean) {
        LOGGER.info("queryPageBean的信息是{}", queryPageBean);
        return Result.ok(purchaseOrderGoodsService.findPage(queryPageBean));
    }


    /**
     * 信息
     */
    @ApiOperation(value="信息")
    @GetMapping("/info/{purchaseOrderGoodsId}")
    public Result info(@PathVariable("purchaseOrderGoodsId") Integer purchaseOrderGoodsId){
		PurchaseOrderGoodsEntity purchaseOrderGoods = purchaseOrderGoodsService.getById(purchaseOrderGoodsId);

        return Result.ok(purchaseOrderGoods);
    }

    /**
     * 保存
     */
    @ApiOperation(value="保存")
    @PostMapping("/save")
    public Result save(@RequestBody PurchaseOrderGoodsEntity purchaseOrderGoods){
		purchaseOrderGoodsService.save(purchaseOrderGoods);

        return Result.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value="修改")
    @PostMapping("/update")
    public Result update(@RequestBody PurchaseOrderGoodsEntity purchaseOrderGoods){
		purchaseOrderGoodsService.updateById(purchaseOrderGoods);

        return Result.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value="删除")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody Integer[] purchaseOrderGoodsIds){
		purchaseOrderGoodsService.removeByIds(Arrays.asList(purchaseOrderGoodsIds));

        return Result.ok();
    }

    /**
     * 提交采购单
     * @Author: xianhao
     * @Date: 2022/4/27 17:47
     * @param map:
     * @return: com.woniuxy.commons.result.Result
     **/
    @ApiOperation("提交采购单")
    @PutMapping("/insert")
    public Result insert(@RequestBody Map<String,Integer> map ){
        Integer x1 = map.get("x1");
        Integer x2 = map.get("x2");
        Integer purchaseOrderId = map.get("purchaseOrderId");
        boolean b = purchaseOrderGoodsService.updateByEdit(x1,x2,purchaseOrderId);
        if (!b) return Result.fail("保存失败");
        return Result.ok("保存成功");
    }



}
